<template>
    <div id="tmpl">

        <div id="postcomment">
            <h3>提交评论</h3>
            <p class=".p"></p>
            <textarea placeholder="请输入你要评论的内容" v-model="postcontent"></textarea>
            <mt-button type="primary" size="large" @click="postcomment">发表</mt-button>
        </div>
          

        <div id="list">
            <h3>评论列表</h3>
            <p class="p"></p>
            <div v-for="(item,index) in list">
                <div class="title">
                    <span>第{{index+1}}楼:</span>
                    <span>用户:{{item.username}}</span>
                    <span>{{item.add_date| datefmt('YYYY-MM-DD HH:mm:ss')}}</span>
                </div>
                <ul class="mui-table-view">
                    <li class="mui-table-view-cell">{{item.content}}</li>
                </ul>
            </div>
        </div>
        <mt-button type="danger" size="large" plain @click="getmore">加载更多</mt-button>
    </div>
</template>

<script>

import con from '../../kits/common.js';
import {Toast} from 'mint-ui';
    export default {
        data(){
            return {
                postcontent:'',
                list:[],
            }
        },
        props:['id'],       //作用是用来接收父组件传递过来的ID值
        methods:{
            getmore(){
                this.pageindex++;

                this.getcommentlist(this.pageindex);
            },
            getcommentlist(pageindex){
                pageindex = pageindex || 1;
                var url = con.base+ "/api/getcomments/"+ this.id+'/'+pageindex;

                this.$http.get(url).then(function(res){
                    var data  = res.body;
                    if(data.status != 0){
                        Toast({message:data.message,duration:5000});
                        return ;
                    }

                    this.list = this.list.concat(data.message);
                    
                })
            },
            postcomment(){

                if(this.postcontent.trim().length <=0){
                    Toast({message:'你还没有输入评论内容',duration:5000});
                    return;
                }

                var url = con.base+ '/api/postcomment/'+this.id;

                console.log('ajax数据提交')
                this.$http.post(url,{content:this.postcontent},{emulateJSON:true}).then(function(response){
                    var data = response.body;
                    
                    if(data.status != 0){
                        Toast({message:data.message,duration: 5000})        //失败提示
                        return;
                    }
                    Toast({message:data.message,duration: 5000})        //提交成功提示

                    this.list  = [{
                        "username":"匿名用户",
                        "add_date": new Date(),
                        "content" : this.postcontent
                    }].concat(this.list);

                    this.postcontent ='';

                })
            }
        },
        created(){
            // this.list = [
            //     {
            //         "username":"匿名用户",
            //         "add_time":"2019-04-09T08:28:08.000Z",
            //         "content":"1111adddsfadsfads"
            //     },{
            //         "username":"匿名用户",
            //         "add_time":"2019-04-09T08:28:08.000Z",
            //         "content":"222adddsfadsfads"
            //     }]
            this.getcommentlist(1);
        }
    }
</script>

<style scoped>

#postcomment{
    padding: 5px;

}
.p{
    height: 1px;
    width: 100%;
    border-bottom: 1px solid rgba(0, 0, 0, 0.3);
}

.title{
        padding: 5px;
        color: #6d6d72;
        font-size: 15px;
        background-color: rgba(0, 0, 0, 0.1);
    }
</style>

